<template>
  <div class="order-page">
    <van-nav-bar title="生成订单" left-arrow @click-left="$router.back()">
      <template #right>
        <van-icon name="ellipsis" size="18" />
      </template>
    </van-nav-bar>

    <div class="address-wrap">
      <div class="name">
        <span>{{ addressDedail.userName }} </span>
        <span>{{ addressDedail.userPhone }}</span>
      </div>
      <div class="address">
        {{ addressDedail.provinceName }} {{ addressDedail.cityName }}
        {{ addressDedail.regionName }} {{ addressDedail.detailAddress }}
      </div>
      <van-icon class="arrow" name="arrow" @click="goaddress" />
    </div>

    <van-swipe-cell v-for="item in goodsDedail" :key="item.goodsId">
      <van-card
        :price="item.sellingPrice"
        :title="item.goodsName"
        :num="item.goodsCount"
      >
        <template #thumb>
          <van-image width="90" height="90" :src="item.goodsCoverImg" />
        </template>
      </van-card>
    </van-swipe-cell>

    <div class="pay-wrap">
      <div class="price">
        <span>商品金额</span>
        <span>¥{{ sum }}</span>
      </div>
      <van-button
        @click="createOrder"
        class="pay-btn"
        color="#1baeae"
        type="primary"
        block
        >生成订单</van-button
      >
    </div>

    <van-popup
      v-model="show"
      closeable
      position="bottom"
      :style="{ height: '30%' }"
      @close="close"
    >
      <div :style="{ width: '90%', margin: '0 auto', padding: '50px 0' }">
        <van-button
          :style="{ marginBottom: '10px' }"
          color="#1989fa"
          block
          @click="payOrder(1)"
          >支付宝支付</van-button
        >
        <van-button color="#4fc08d" block @click="payOrder(2)"
          >微信支付</van-button
        >
      </div>
    </van-popup>
  </div>
</template>

<script>
import { Dialog } from "vant";
export default {
  data() {
    return {
      show: false,
      chosenAddressId: "1",
      list: [],
      cartItemIds: [],
      addressDedail: [],
      goodsDedail: [],
      sum: 0,
      orderNo: "",
    };
  },
  //   取消下面的导航栏
  mounted() {
    this.$parent.showTabbar = false;
  },
  destroyed() {
    this.$parent.showTabbar = true;
  },
  created() {
    this.refresh();
  },
  methods: {
    async refresh() {
      // 获取购物车的订单号和地址
      const { addressId } = this.$route.query;

      //获取地址和添加地址
      const { data: address } = addressId
        ? await this.$axios.get("/address", addressId)
        : await this.$axios.get("/address/default");
      this.addressDedail = address;
      if (address.length == 0) {
        Dialog.confirm({
          message: "请添加地址",
          theme: "round-button",
        }).then(() => {
          this.$router.push("/address");
        });
        return;
      }
      // 获取当前需要购买的商品
      const goodsId = localStorage.getItem("goodsId");
      let arr = goodsId.split(",");
      this.$axios
        .get("/shop-cart", {
          pageNumber: 1,
        })
        .then((data) => {
          this.goodsDedail = data.data.filter((item) => {
            return arr.includes("" + item.goodsId);
          });
          this.goodsDedail.forEach((item) => {
            this.sum += item.goodsCount * item.sellingPrice;
          });
        });
    },
    // 生成订单需要当前数据的id和当前数据对应的地址
    createOrder() {
      this.show = true;
      const params = {
        addressId: this.addressDedail.addressId,
        cartItemIds: this.goodsDedail.map((item) => item.cartItemId),
      };
      // 获取订单号
      this.$axios.post("/saveOrder", params).then((data) => {
        this.orderNo = data.data;
        localStorage.setItem("goodsId", "");
      });
    },
    // 提交订单
    payOrder(type) {
      console.log(this.orderNo);
      let typePay = String(type);
      console.log(typePay);
      this.$axios
        .get("/paySuccess", {
          params: {
            orderNo: this.orderNo,
            payType: typePay,
          },
        })
        .then((data) => {
          console.log(data);
        });

      this.$router.push("/order");
    },
    // 关闭订单
    close() {
      this.$router.push("/order");
    },

    goaddress() {
      this.$router.push("/address");
    },
  },
};
</script>

<style lang="scss" scoped>
.order-page {
  .address-wrap {
    margin-bottom: 20px;
    background: #fff;
    position: relative;
    font-size: 14px;
    padding: 15px;
    color: #222333;
    .name,
    .address {
      margin: 10px 0;
    }
    .arrow {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 20px;
    }
    &::before {
      position: absolute;
      right: 0;
      bottom: 0;
      left: 0;
      height: 2px;
      background: -webkit-repeating-linear-gradient(
        135deg,
        #ff6c6c 0,
        #ff6c6c 20%,
        transparent 0,
        transparent 25%,
        #1989fa 0,
        #1989fa 45%,
        transparent 0,
        transparent 50%
      );
      background: repeating-linear-gradient(
        -45deg,
        #ff6c6c 0,
        #ff6c6c 20%,
        transparent 0,
        transparent 25%,
        #1989fa 0,
        #1989fa 45%,
        transparent 0,
        transparent 50%
      );
      background-size: 80px;
      content: "";
    }
  }
  .pay-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
    padding: 10px 0;
    padding-bottom: 50px;
    border-top: 1px solid #e9e9e9;
    > div {
      display: flex;
      justify-content: space-between;
      padding: 0 5%;
      margin: 10px 0;
      font-size: 14px;
      span:nth-child(2) {
        color: red;
        font-size: 18px;
      }
    }
    .pay-btn {
      position: fixed;
      bottom: 7px;
      right: 0;
      left: 0;
      width: 90%;
      margin: 0 auto;
    }
  }
}
.van-swipe-cell {
  margin-bottom: 15px;
}
</style>
